<?php

namespace App\Admin\Controllers;

use App\Imports\UsersImport;
use App\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('会员管理')
            ->description('会员列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('会员管理')
            ->description('会员信息')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    public function importView(Content $content)
    {
        $form = new Form(new User);
        $form->setAction('import');
        $form->file('importfile', '文件');

        return $content
            ->header('Import')
            ->description('Import Users')
            ->body($form);
    }

    public function import(){

        $import = new UsersImport();
        $import->import(request()->file('importfile'));
//        return redirect('/')->with('success', 'All good!');
        $message = '';
        foreach ($import->failures() as $failure) {
            $message .= '第'.($failure->row()).'行有误'.implode(',',$failure->values()).implode(',',$failure->errors()).'<br>';

//            $failure->row(); // row that went wrong
//            dd($failure->row());
//            $failure->attribute(); // either heading key (if using heading row concern) or column index
//            $failure->errors(); // Actual error messages from Laravel validator
//            $failure->values(); // The values of the row that has failed.
        }
        foreach ($import->errors() as $v){
            $message .= $v->getMessage().'<br>';
        }
        if($message){
            admin_toastr($message, 'error',['timeOut' => 0]);
        }

    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('Id')->sortable();
        $grid->column('avatar.path','头像')->image('',100,100);
        $grid->name('姓名');
        $grid->mobile('手机号');
        $grid->email('邮箱');
//        $grid->remember_token('Remember token');
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');

        $grid->filter(function ($filter) {
            // 设置created_at字段的范围查询
            $filter->between('created_at', 'Created Time')->datetime();
        });

        $grid->header(function ($query) {
            return '<a href="/admin/users/import" class="btn btn-default">导入</a>';
        });

//        $grid->disableColumnSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->email('Email');
        $show->password('Password');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->submitted(function (Form $form) {
        });
        $form->saving(function (Form $form) {
//            $form->password = '888888';
//            var_dump($form->avatar);die;
        });

        $form->saved(function (Form $form) {
//            var_dump($form->avatar);die;
        });

        $form->uploadimg('avatar', '头像')->move('avatar/')->uniqueName();
//        $form->image('avatarpath', '头像')->move('avatar/')->uniqueName();
//        $form->text('test.title', 'Test Title')->value(function($value){
//            return '1234';
//        });
        $form->text('test.name', 'Test Name');
        $form->text('name', 'Name');
        $form->email('email', 'Email');
        $form->password('password', 'Password');
        $form->text('remember_token', 'Remember token');

        return $form;
    }
}
